package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;


    //用于添加购物车
    @PostMapping("/add")
    public R save(@RequestBody ShoppingCart shoppingCart, HttpSession hs) {
        log.info("要添加购物车的菜品为{}", shoppingCart);
        if (shoppingCart.getDishId() == null || shoppingCart.getSetmealId() == null) {
            return R.error("购物车添加失败");
        }
        Long userId = (Long) hs.getAttribute("user");

  /* 菜品或者套餐是否点过
     如何知道烧鹅被点过了？？ 根据烧鹅的菜品id，查询下数据库中有没有烧鹅这个数据
     如何是菜品：select * from shopping_cart where dish_id = ? and user_id = ?
     如果是套餐：select * from shopping_cart where setmeal_id = ? and user_id = ?
     */
        LambdaUpdateWrapper<ShoppingCart> qw = new LambdaUpdateWrapper<>();
        qw.eq(ShoppingCart::getUserId, userId);
        // 判断当前用户点的是菜品还是套餐
        //Long dishId = shoppingCart.getDishId();
        // if (dishId != null) {//说明点的是菜品
        qw.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        // qw.eq(ShoppingCart::getDishId != null, ShoppingCart::getDishId, dishId);
        //  } else {//如果不是菜品，那么一定是套餐
        qw.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        //  }
        ShoppingCart one = shoppingCartService.getOne(qw);
        // 第一次点的时候是新增 ，后面在点击的时候是修改
        if (one == null) {
            // 如果没有，则是新增
            // 设置 number=1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setUserId(userId);
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        } else {
            // 如果有，则是更新
            one.setNumber(one.getNumber() + 1);
            shoppingCartService.updateById(one);
        }
        return R.success(one);
    }

    //用于查看购物车
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        log.info("查看购物车...");

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }

    //用于取消购物车
    @PostMapping("/sub")
    public R removeCart(@RequestBody ShoppingCart shoppingCart) {
        log.info("取消购物车");
        if (shoppingCart.getDishId() == null && shoppingCart.getSetmealId() == null) {
            return R.error("购物车取消失败");
        }
        //判断是菜品还是套餐
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();

        qw.eq(ShoppingCart::getUserId, userId);

        Long dishId = shoppingCart.getDishId();
        if (dishId != null) {//是菜品
            qw.eq(ShoppingCart::getDishId, dishId);// dish_id = ?
        } else {// 是套餐
            qw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());// setmeal_id = ?
        }
        ShoppingCart one = shoppingCartService.getOne(qw);
        // number -1 然后操作：
        Integer number = one.getNumber();
        number--;
        one.setNumber(number);
        if (number > 0) {
            // >0  update更新 update set number=? where id = ?
            shoppingCartService.updateById(one);
        } else {
            // <= 0  如何操作？ delete from shopping_cart where dish_id=? and user_id = ?
            shoppingCartService.remove(qw);
        }
        return R.success(one);
    }

    //用于清空购物车
    @DeleteMapping("/clean")
    public R clean(HttpSession hs) {
        log.info("正在进行清空购物车操作...");
        long userId = (long) hs.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, userId);
        shoppingCartService.remove(qw);
        return R.success("清空购物车成功!");
    }
}
